import { DatabaseHelper } from './DatabaseHelper.js';

let myHandler = async function (event, context, callback, logger) {
  logger.info(JSON.stringify(event))
  let userId: string = event.body ? JSON.parse(event.body).userId : event.userId;
  let pageNum: number = event.body ? JSON.parse(event.body).pageNum : event.pageNum;
  let pageSize: number = event.body ? JSON.parse(event.body).pageSize : event.pageSize;
  let type: string = event.body ? JSON.parse(event.body).type : event.type;

  if (!pageNum || !pageSize || !type) {

    callback({
      code: 1,
      message: 'params can not empty',
      data: null,
    });
  } else {
    try {
      const databaseHelper = new DatabaseHelper(context, logger);
      const result = await databaseHelper.queryResource(userId, type, pageNum, pageSize);
      if (result) {
        callback({
          code: 0,
          message: 'Success',
          data: result,
        });
      } else {
        callback({
          code: 1,
          message: '未找到数据',
          data: null,
        });
      }
    } catch (err) {
      logger.error("func error:" + err.message + " stack:" + err.stack);
      callback({
        code: 3,
        message: '数据查询失败',
        data: err,
      });
    }
  }
};

export { myHandler };